package edu.just.pet.controller;

import com.github.pagehelper.PageInfo;
import edu.just.pet.dao.AdopRecordDao;
import edu.just.pet.entity.AdopRecord;
import edu.just.pet.entity.Cat;
import edu.just.pet.entity.Dog;
import edu.just.pet.service.AdopRecordService;
import edu.just.pet.service.CatService;
import edu.just.pet.service.DogService;
import edu.just.pet.system.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("record")
public class AdopRecordController {
    @Autowired
    private AdopRecordService adopRecordService;
    @Autowired
    private CatService catService;
    @Autowired
    private DogService dogService;
    @RequestMapping("index.do")
    public ModelAndView index() {
        Map<String, Object> result = new HashMap<>();

        List<AdopRecord> adopRecordList = adopRecordService.getAdopRecordListByPage(new AdopRecord());

        result.put(Constants.RETURN_DATA, adopRecordList);
        result.put(Constants.RETURN_CODE, Constants.CODE_SUCCESS);
        result.put(Constants.RETURN_MSG, "请求成功");

        return new ModelAndView("/back/adop_record", result);
    }

    @RequestMapping("getRecordList.do")
    @ResponseBody
    public Map getRecordList(AdopRecord adopRecord){
        Map<String, Object> result = new HashMap<>();
        List<AdopRecord> adopRecordList=adopRecordService.getAdopRecordListByPage(adopRecord);

        PageInfo<AdopRecord> pageInfo=new PageInfo<>(adopRecordList);
        result.put(Constants.RETURN_DATA, pageInfo.getList());
        result.put(Constants.RETURN_COUNT, pageInfo.getTotal());

        result.put(Constants.RETURN_CODE, Constants.CODE_SUCCESS);
        result.put(Constants.RETURN_MSG, "请求成功");
        return result;
    }
    @RequestMapping("add.do")
    @ResponseBody
    public Map<String,Object> addRecord(AdopRecord adopRecord){
        Map<String,Object> result =new HashMap<>();

        if (adopRecord.getAdopterId()==null){

            result.put(Constants.RETURN_CODE,"3");
            result.put(Constants.RETURN_MSG,"请先登录！");
            return result;
        }
        if (adopRecord.getType()==1){
            Cat cat=new Cat();
            cat.setId(adopRecord.getPetId());
            cat.setAdopter(adopRecord.getAdopterId());
            catService.adop(cat);
        }
        if (adopRecord.getType()==2){
            Dog dog=new Dog();
            dog.setId(adopRecord.getPetId());
            dog.setAdopter(adopRecord.getAdopterId());
            dogService.adop(dog);
        }
        System.out.println("record:"+adopRecord);

            int i=adopRecordService.addRecord(adopRecord);

            if (i>0){
                result.put(Constants.RETURN_CODE,Constants.CODE_SUCCESS);
                result.put(Constants.RETURN_MSG,Constants.MSG_SUCCESS);
            }else {
                result.put(Constants.RETURN_CODE,Constants.CODE_ERROR);
                result.put(Constants.RETURN_MSG,Constants.MSG_ERROR);
            }
        return result;
    }


}
